import java.net.*;
import java.io.*;
import java.lang.Character;

class TrackerThread implements Runnable{

	private Thread _t; 
	public Socket _s; 
	private PrintWriter _out; 
	private BufferedReader _in;
	public Tracker _tracker;
	public int _numClient=0;
	public Integer _port=null;


	TrackerThread(Socket s, Tracker tracker){
		_tracker=tracker; 
		_s=s;
		try{
			_out = new PrintWriter(_s.getOutputStream());
			_in = new BufferedReader(new InputStreamReader(_s.getInputStream()));
			_numClient = tracker.addClient(_out);
		}
		catch (IOException e){ }
		_t = new Thread(this);
		_t.start();
	}


	public void run(){
		String message = ""; 
		String answer = "";
		int readingstate=0;
		int getfileint=0;
		System.out.println("Un nouveau client s'est connecte, no "+_numClient);
		try{
			char charCur[] = new char[1]; 
			while(_in.read(charCur, 0, 1) >= 0){
				switch (readingstate) {
				case(0):{
					if (charCur[0] != ' ' && charCur[0] != '\n'){
						message += charCur[0];
						readingstate=1;
					}
					break;
				}
				case(1):{
					message += charCur[0];
					if (Character.isWhitespace(charCur[0])){
						//test du 1er mot
						if(message.equals("look ")){
							readingstate=5;
						}
						else if(message.equals("update ") || message.equals("announce ")){
							readingstate=4;
						}
						else if(message.equals("getfile ")){
							readingstate=2;
							getfileint=0;
						}
						else {
							readingstate=-1;
						}
					}
					break;
				}
				case(2):{
					message += charCur[0];
					getfileint ++;
					if(getfileint >= 32){
						System.out.println("Client no "+_numClient+" : "+message);
						answer=_tracker._bdd.answer(message,this);
						if(_port == null){
							_out.print("KO");
						}
						else{
							//Voir pour ne faire l'appelle qu'une fois
							System.out.println("answer (port ="+_port+") : "+answer+"\n");
							_out.print(answer);
						}
						_out.flush();
						readingstate=0;
						message = "";	
					}
					break;
				}
				case(4):{
					message += charCur[0];
					if (charCur[0] == ']'){
						readingstate=5;
					}
					break;
				}
				case(5):{
					message += charCur[0];
					if (charCur[0] == ']'){
						if(!message.equalsIgnoreCase("")){
							System.out.println("Client no "+_numClient+" : "+message);
							answer=_tracker._bdd.answer(message,this);
							if(_port==null){	
								_out.print("KO");
							}
							else{
								//Voir pour ne faire l'appelle qu'une fois
								System.out.println("answer (port ="+_port+") : "+answer+"\n");
								_out.print(answer);	
							}
							_out.flush();
							readingstate=0;
							message = "";
						}	
					}
					break;
				}
				default:{
					System.out.println("Problem in parsing items\n");
					_out.print("KO");
					_out.flush();
					break;
				}
				}
			}
		}
		catch (Exception e){ }
		finally{
			try{
				if(_port!=null)
					_tracker._bdd.removeAll(_s.getInetAddress().getHostAddress(),_port);
				System.out.println("Le client no "+_numClient+" s'est deconnecte");
				_tracker.delClient(_numClient);
				_s.close();
			}
			catch (IOException e){ }
		}
	}
}
